package com.tian.hello.ui.qqfriend;

import android.os.Bundle;
import com.google.android.material.tabs.TabLayout;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import androidx.appcompat.app.AppCompatActivity;
import android.util.SparseArray;

import com.tian.hello.R;

/**
 * QQ联系人,注意应该尽量多的使用约束布局{@link ConstraintLayout},减少嵌套，这里时间有限只在partly_newfriends中使用了
 * <p>@author：skyTian
 * <br>@time:2018/2/1
 * <br>@copyright:com.sky
 */
public class QQActivity extends AppCompatActivity {
    /**
     * 顶部栏
     */
    TabLayout tab_title;
    /**
     * ViewPager
     */
    ViewPager vp_pager;

    /**
     * 切换的fragment集合
     */
    private SparseArray<Fragment> fragments;

    /**
     * ViewPage的适配器
     */
    private SectionsPagerAdapter sectionsPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qq);
        init();
    }


    private void init() {

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


        tab_title = findViewById(R.id.tab_title);
        vp_pager = findViewById(R.id.vp_pager);

        fragments = new SparseArray<>();
        sectionsPagerAdapter = new SectionsPagerAdapter(this.getSupportFragmentManager());

        tab_title.addTab(tab_title.newTab().setText("好友"), true);
        tab_title.addTab(tab_title.newTab().setText("群聊"), true);
        tab_title.addTab(tab_title.newTab().setText("设备"), true);
        tab_title.addTab(tab_title.newTab().setText("通讯录"), true);
        tab_title.addTab(tab_title.newTab().setText("公众号"), true);

        vp_pager.setAdapter(sectionsPagerAdapter);
        tab_title.setupWithViewPager(vp_pager);
    }


    /**
     * ViewPageAdapter
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {
        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return getFragment(position);
        }

        @Override
        public int getCount() {
            return 5;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "好友";
                case 1:
                    return "群聊";
                case 2:
                    return "设备";
                case 3:
                    return "通讯录";
                case 4:
                    return "公众号";
            }
            return "";
        }
    }


    /**
     * 获取Fragment
     *
     * @param index
     * @return
     */
    private Fragment getFragment(int index) {
        Fragment fragment = fragments.get(index);
        if (fragment == null) {
            fragment = QQFriendsFragment.newInstance(null);
            fragments.put(index, fragment);
        }
        return fragment;
    }

}
